﻿package rob.maths{
	import rob.core.World3d;
	import rob.data.Vert3d;
	import rob.data.Vect3d;
	import rob.core.Camera3d;
	import rob.data.Basis3d;
	
	public class VertexMaths extends VectorMaths{
		
		public function VertexMaths():void{
		}
		
		public static function sub(v:Vert3d,w:Vert3d):Vert3d{
			return new Vert3d(v.x - w.x,v.y - w.y,v.z - w.z);
		}
		
		
		public static function subTo(v:Vert3d,w:Vert3d):void{
			v.x -= w.x;	v.y -= w.y;	v.z -= w.z;
		}
		

		public static function addTo(v:Vect3d,w:Vect3d):void{
			v.x += w.x;	v.y += w.y;	v.z += w.z;
		}
		
		public static function scaleTo(k:Number,v:Vert3d):void{
			v.x *= k;	v.y *= k;	v.z *= k;
		}
		
		public static function scaleVectTo(sv:Vect3d,v):void{
			v.x *= sv.x;	v.y *= sv.y;	v.z *= sv.z;
		}
		
		public static function average(a:Array):Vert3d{
			var avg:Vert3d = new Vert3d(0,0,0);
			for each(var p:Vert3d in a) VertexMaths.addTo(avg,p);
			VertexMaths.scaleTo(1/a.length,avg);
			return avg;
		}

		
		public static function project(p:Vert3d,c:Camera3d):void{
			var rel:Vert3d = sub(p,c.position);
			var wx:Number, wy:Number, wz:Number;
			var b:Basis3d = c.basis, sz = c.sz;
			
			p.wx = wx = dp(rel,b.i);
			p.wy = wy = dp(rel,b.j);
			p.wz = wz = dp(rel,b.k);

			///*
			p.sx = -sz * (wx/wz);
			p.sy = sz * (wy/wz);
			//*/
			//p.sx = .5*wx;	p.sy = -.5*wy;
			
			p.cd = -(rel.x * rel.x + rel.y * rel.y + rel.z *rel.z);
			//p.cd = wz - 0.1*(Math.abs(p.sx) + Math.abs(p.sy));
		}
	}
}